package com.example.learnvien.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.learnvien.R;
import com.example.learnvien.data.LetterItemData;

public class GridViewLetterAdapter extends BaseAdapter
{
    private List<LetterItemData> mItemDatas = new ArrayList<LetterItemData>();
    private Context mContext;

    public GridViewLetterAdapter(Context context, List<LetterItemData> datas)
    {
        mContext = context;
        for (LetterItemData letterItemData : datas)
        {
            mItemDatas.add(letterItemData);
        }
    }

    @Override
    public int getCount()
    {
        return mItemDatas.size();
    }

    @Override
    public Object getItem(int arg0)
    {
        return mItemDatas.get(arg0);
    }

    @Override
    public long getItemId(int arg0)
    {
        return 0;
    }

    @Override
    public View getView(int position, View arg1, ViewGroup arg2)
    {
        View view = ((Activity) mContext).getLayoutInflater().inflate(
                R.layout.letter_item, null);

        TextView letterView = (TextView) view.findViewById(R.id.letter);
        ImageView letterIconView = (ImageView) view.findViewById(R.id.letter_icon);
        TextView phraseView = (TextView) view.findViewById(R.id.phrase);

        LetterItemData data = (LetterItemData) getItem(position);
        if (data == null)
        {
            return null;
        }

        letterView.setText(data.mLetter);
        phraseView.setText(data.mPhrase);
        letterIconView.setImageDrawable(data.mImageIcon);

        return view;
    }

    public void updateDatas(List<LetterItemData> items)
    {
        if(items == null)
        {
            return;
        }
        mItemDatas.clear();
        for (LetterItemData letterItemData : items)
        {
            mItemDatas.add(letterItemData);
        }
        notifyDataSetChanged();
    }
}
